﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Applee.FractCommon;
using System.Drawing;

namespace Applee.SierpinskiFract
{
    class RectangleFract3 : BaseFract
    {
        protected override void Initialize()
        {
            SizeF shapeSize = new SizeF(1.0F, 1.0F); //box边比1:1
            float flength = 0;
            float fMargin = 10;

            if (MathUtils.CalcAdaptiveLength(mViewSize, shapeSize, ref flength))
            {
                //width
                mLength = 1.0F * flength - fMargin * 2.0F;
            }
            else
            {
                //Height
                mLength = 1.0F * flength - fMargin * 2.0F;
            }
        }

        protected override void CreateFract(PointF point, RandomColor color, float fLength, int iterations)
        {
            if (iterations < 0)
                return;

            if (fLength < 1.0F)
                return;

            foreach (IShape so in mFractObjects)
            {
                float d = MathUtils.Distance(so.Points[0], so.Points[1]);
                if (d > fLength && so.Color != Color.White)
                    so.Color = Color.White;
            }

            SierRectangle et1 = new SierRectangle();
            et1.MakeShape(point, fLength);
            et1.Color = color.GetColor(iterations);

            mFractObjects.Add(et1);

            PointF point1 = new PointF(point.X, point.Y - fLength * 0.25F);
            CreateFract(point1, color, fLength * 0.5F, iterations - 1);

            float flength0 = fLength * 0.25F;

            PointF point2 = new PointF(point.X + flength0, point.Y + flength0);
            CreateFract(point2, color, fLength * 0.5F, iterations - 1);

            PointF point3 = new PointF(point.X - flength0, point.Y + flength0);
            CreateFract(point3, color, fLength * 0.5F, iterations - 1);
        }
    }
}
